import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

Vue.use(Vuex);

export const store = new Vuex.Store({
    state:{
        newsList:[],
        productList:[],
        sportsNews:[],
        scienceNews:[],
        carNews:[],
        governmentNews:[]
    },
    getters:{
        newsList:state=>{
            return state.newsList
        },
        productList:state=>{
            return state.productList
        },
        sportsNews:state=>{
            return state.sportsNews
        },
        scienceNews:state=>{
            return state.scienceNews
        },
        carNews:state=>{
            return state.carNews
        },
        governmentNews:state=>{
            return state.governmentNews
        }
    },
    mutations:{
        getNewsList:(state,payload)=>{
            axios.post(`http://localhost:3000/news/list`,payload)
            .then(res=>{
                console.log(state.newsList)
                state.newsList=res.data.rows
            })
        },
        getproductList:(state,payload)=>{
            axios.post(`http://localhost:3000/product/list`,payload)
            .then(res=>{
                console.log(state.productList)
                state.productList=res.data.rows
            })
        },
        getsportsNews:(state,payload)=>{
            axios.get(`http://localhost:3000/news/data/news/59f831185f19fc053872eca1`,payload)
            .then(res=>{
                console.log(state.sportsNews)
                state.sportsNews=res.data
            })
        },
        getscienceNews:(state,payload)=>{
            axios.get(`http://localhost:3000/news/data/news/59f8311b5f19fc053872eca2`,payload)
            .then(res=>{
                console.log(state.scienceNews)
                state.scienceNews=res.data
            })
        },
        getcarNews:(state,payload)=>{
            axios.get(`http://localhost:3000/news/data/news/59f8321c5f19fc053872eca8`,payload)
            .then(res=>{
                console.log(state.carNews)
                state.carNews=res.data
            })
        },
        getgovernmentNews:(state,payload)=>{
            axios.get(`http://localhost:3000/news/data/news/59f832455f19fc053872ecab`,payload)
            .then(res=>{
                console.log(state.governmentNews)
                state.governmentNews=res.data
            })
        }

    },
    actions:{
        getNewsList:({commit},payload)=>{
            commit('getNewsList',payload)
        },
        getproductList:({commit},payload)=>{
            commit('getproductList',payload)
        },
        getsportsNews:({commit},payload)=>{
            commit('getsportsNews',payload)
        },
        getscienceNews:({commit},payload)=>{
            commit('getscienceNews',payload)
        },
        getcarNews:({commit},payload)=>{
            commit('getcarNews',payload)
        },
        getgovernmentNews:({commit},payload)=>{
            commit('getgovernmentNews',payload)
        }
    }
})

